﻿var cvis = {
    gtool: {
        // pos:{x,y}
        // size:{w,h}
        // targetX,
        // targetY
        containsPSXY: function (pos, size, targetX, targetY) {
            if (pos.x < targetX && pos.y < targetY
                && pos.x + size.w > targetX
                && pos.y + size.h > targetY) {
                return true;
            }
            return false;
        },
        containsXYWHXY: function (x, y, w, h, targetX, targetY) {
            if (x < targetX && y < targetY
                && x + w > targetX
                && y + h > targetY) {
                return true;
            }
            return false;
        },
        // 计算左上角的位置
        calcPosWHWH: function (alignH, alignV, contentW, contentH, containerW, containerH) {
            var x = 0;
            var y = 0;
            if (alignH == 'center') {
                x = (containerW - contentW) / 2;
            }

            if (alignV == 'center') {
                y = (containerH - contentH) / 2;
            }
            return { x: x, y: y };
        }
    },
    com: {
        //
        //
        // 
        // func(item, {index, length})
        foreach: function (array, func) {
            for (var index = 0; index < array.length; index++) {
                func(array[index], { index: index, length: array.length });
            }
        }
    },
    res: {
        _baseUrl: null,
        get: function (relativeUrl) {
            return getUrl(relativeUrl);
        },
        getUrl: function (relativeUrl) {
            if (this._baseUrl) {
                return this._baseUrl + relativeUrl;
            } else {
                return relativeUrl;
            }
        },
        setBaseUrl: function (baseUrl) {
            this._baseUrl = baseUrl;
        }
    },
    data: {
        _baseUrl: null,
        getUrl: function (relativeUrl) {
            if (this._baseUrl) {
                return this._baseUrl + relativeUrl;
            } else {
                return relativeUrl;
            }
        },
        setBaseUrl: function (baseUrl) {
            this._baseUrl = baseUrl;
        }
    },
    event: {
        _eventDict: [],
        sendEvent: function (name, dataString) {
            if ("undefined" != typeof CZScene) {
                CZScene.netEventSend(name, dataString);
            } else {
                this.callback(name, dataString);
                //for (var index = 0; index < this._eventDict.length; index++) {
                //    if (this._eventDict[index].name == name) {
                //        this._eventDict[index].callback(name, dataString);
                //        return;
                //    }
                //}
            }
        },
        registerEvent: function (name, callback) {
            for (var index = 0; index < this._eventDict.length; index++) {
                if (this._eventDict[index].name == name) {
                    return;
                }
            }
            this._eventDict.push({ name: name, callback: callback });
        },
        callback: function (name, dataString) {
            for (var index = 0; index < this._eventDict.length; index++) {
                if (this._eventDict[index].name == name) {
                    this._eventDict[index].callback(name, dataString);
                    return;
                }
            }
        }
    },
    engine: {
        _node: [],
        _size:null,
        _ctx: null,
        //{canvas:
        // size:
        // interval
        //}
        start: function(config){
            this._ctx = config.canvas.getContext("2d");
            this._size = config.size;
            this._interval = config.interval;
            var interval = this._interval;
            window.setInterval(function () {
                cvis_timer_tick(interval);
            }, this._interval);
        },
        render: function (ctx, delta) {
            console.log('cvis.render:'+ delta);
        },
        update: function (delta) {
            console.log('cvis.update:' + delta);
        },
        loop: function (delta) {
            this.update(delta);
            this.render(null, delta);
        },
        add: function (node) {
            this._node.push(node);
        }
    }
};

function cvis_timer_tick(delta) {
    cvis.engine.loop(delta);
}

function cvisNode(option) {
    this.option = option;
    this.render = function () {
    }
}